package com.example.administrator.smartdevice.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.base.BaseActivity;
import com.example.administrator.smartdevice.model.MemberModel;
import com.example.administrator.smartdevice.net.HttpUtils;
import com.example.administrator.smartdevice.utils.AgentConstants;
import com.example.administrator.smartdevice.utils.AgentSharedPreferences;
import com.example.administrator.smartdevice.utils.AgentUtils;
import com.example.administrator.smartdevice.utils.AppApplication;
import com.example.administrator.smartdevice.utils.DisplayHelper;
import com.example.administrator.smartdevice.utils.MD5;
import com.example.administrator.smartdevice.utils.ServerConstants;
import com.example.administrator.smartdevice.utils.StringUtils;
import com.example.administrator.smartdevice.widget.CustomTipDialog;


import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.activity_login_edit_telephone)
    protected EditText activity_login_edit_telephone;

    @BindView(R.id.activity_login_edit_password)
    protected EditText activity_login_edit_password;

    protected String memberTelephone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initData() {
        //设置状态栏颜色
        DisplayHelper.setStatusBarColor(mBaseActivity, R.color.common_head);
        if (AgentConstants.Action.EXIT_APP.equals(getIntent().getAction())) {
            finish();
            AppApplication.exit();
        } else {
            initLastLoginId();
        }
    }

    @Override
    protected void onFailure(int requestCode, Object object) {
        showToast(R.string.common_response_failure);
        stopTipDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        if (object != null) {
            switch (requestCode) {
                case 1001:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                        startOtherActivity(MainActivity.class, null);
                        MemberModel memberModel = (MemberModel) HttpUtils.getData(object, MemberModel.class);
                        AgentSharedPreferences.saveMemberInfo(mBaseActivity, memberModel);
                        AgentSharedPreferences.setLastLoginId(mBaseActivity, memberModel.getMemberTelephone());
                        AgentSharedPreferences.setLastDeviceId(mBaseActivity, memberModel.getMemberDeviceId());
                    } else {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
            }
        }
    }

    @OnClick(R.id.activity_login_bt_login)
    protected void loginClick() {
//        startOtherActivity(MainActivity.class,null);
        memberTelephone = activity_login_edit_telephone.getText().toString();
        String memberPassword = activity_login_edit_password.getText().toString();
        if (memberTelephone.indexOf(" ") != -1) {
            memberTelephone = memberTelephone.replace(" ", "");
        }
        if (StringUtils.isBlank(memberTelephone)) {
            showToast(R.string.toast_telephone_null);
            return;
        }
        if (!AgentUtils.isMobile(memberTelephone)) {
            showToast(R.string.toast_telephone_format_warn);
            return;
        }

        if (StringUtils.isBlank(memberPassword)) {
            showToast(R.string.toast_password_null);
            return;
        }
        doLogin(memberTelephone, memberPassword);
    }

    protected void doLogin(String memberTelephone, String memberPassword) {
        String MD5Password = MD5.encryptToMD5(memberPassword);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberTelephone", memberTelephone);
        jsonObject.put("memberPassword", MD5Password);
        HttpUtils.post(this, 1001, ServerConstants.LOGIN, jsonObject.toString(), this);
        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.login_loading), true);
    }

    protected void initLastLoginId() {
        String memberTelephone = AgentSharedPreferences.getLastLoginId(mBaseActivity);
        if (memberTelephone != null && !memberTelephone.equals("")) {
            activity_login_edit_telephone.setText(memberTelephone);
            activity_login_edit_password.setFocusable(true);
            activity_login_edit_password.setFocusableInTouchMode(true);
            activity_login_edit_password.requestFocus();//获取焦点
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (AgentConstants.Action.EXIT_APP.equals(intent.getAction())) {
            finish();
            AppApplication.exit();
        }
    }
}
